package com.jswdwsx.esign.entity

import com.vladmihalcea.hibernate.type.json.JsonStringType
import com.jswdwsx.esign.cloud.enums.IdTypeEnum
import com.jswdwsx.esign.cloud.enums.IdentityTypeEnum
import com.jswdwsx.esign.cloud.enums.OrgIdTypeEnum
import com.jswdwsx.esign.cloud.enums.SealEnum
import org.hibernate.annotations.*
import org.hibernate.annotations.Parameter
import org.springframework.data.annotation.CreatedDate
import org.springframework.data.annotation.LastModifiedDate
import org.springframework.data.jpa.domain.support.AuditingEntityListener
import java.time.LocalDateTime
import javax.persistence.*
import javax.persistence.Entity
import javax.persistence.Table

@Entity
@EntityListeners(AuditingEntityListener::class)
@DynamicInsert
@DynamicUpdate
@Table(name = "request")
@TypeDef(name = "json", typeClass = JsonStringType::class)
class RequestPO(

    @Id
    @GeneratedValue(generator = "jpa-uuid")
    @GenericGenerator(name = "jpa-uuid", strategy = "uuid")
    var id: String? = null,

    @Column(name = "app_id")
    var appId: String,

    @Column(name = "app_serial_number")
    var appSerialNumber: String,

    @Column(name = "flow_template_id")
    var templateId: String,

    @Column(name = "signer_name")
    var signerName: String? = null,

    @Column(name = "signer_id_type")
    @Enumerated(EnumType.STRING)
    var signerIdType: IdTypeEnum? = null,

    @Column(name = "signer_id_number")
    var signerIdNumber: String? = null,

    @Column(name = "signer_mobile")
    var signerMobile: String? = null,

    @Column(name = "identity_type")
    @Enumerated(EnumType.STRING)
    var identityType: IdentityTypeEnum,

    @Column(name = "org_name")
    var orgName: String? = null,

    @Column(name = "org_id_type")
    @Enumerated(EnumType.STRING)
    var orgIdType: OrgIdTypeEnum? = null,

    @Column(name = "org_id_number")
    var orgIdNumber: String? = null,

    @Column(name = "status")
    @Enumerated(EnumType.STRING)
    var status: RequestStatusEnum,

    //@Transient
    @Type(type = "json")
    var fileFillParam: Map<String, String> = emptyMap(),

    @Type(type = "json")
    var seals: Map<SealEnum, String> = emptyMap(),

    // 异步返回签署结果的回调url
    @Column(name = "notify_url")
    var notifyUrl: String?,

    // 签署完成页面重定向的url
    @Column(name = "redirect_url")
    var redirectUrl: String?,

    @CreatedDate
    @Column(name = "created_time")
    var createdTime: LocalDateTime? = null,

    @LastModifiedDate
    @Column(name = "updated_time")
    var updatedTime: LocalDateTime? = null
)

enum class RequestStatusEnum {
    Created,        // "待处理"
    Waiting,        // "待签署"
    Success,        // "签署成功"
    Fail        // "签署失败"
}
